package com.sky.controller.user;

import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.PushBuilder;
import java.util.List;

@RestController("userSetmealController")
@RequestMapping("/user/setmeal")
public class SetmealController {
    @Resource
    private SetmealService setmealService;
    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    public Result<List<Setmeal>> list(Long categoryId){
        String key = "setmeal_"+categoryId;
        List<Setmeal> list =(List<Setmeal>) redisTemplate.opsForValue().get(key);
        if (list != null && list.size() > 0){
            return Result.success(list);
        }

        list = setmealService.list(categoryId);
        redisTemplate.opsForValue().set(key,list);
        return Result.success(list);
    }

    @GetMapping("/dish/{id}")
    public Result<List<DishItemVO>> dish(@PathVariable Long id){
        String key = "dishTtemVO_"+id;
        List<DishItemVO> list =(List<DishItemVO>) redisTemplate.opsForValue().get(key);
        if (list != null && list.size() > 0){
            return Result.success(list);
        }
        list = setmealService.getSetmealDishBySetmealId(id);
        redisTemplate.opsForValue().set(key,list);
        return Result.success(list);
    }
}
